﻿function BaseVm() {
    var base = this;
    //If we've already newed up the base class let's just grab it.
    if (window.baseVm != null) {
        for (var prop in window.baseVm) {
            base[prop] = window.baseVm[prop];
        }
        return;
    }

    base.PerformingServerCall = ko.observable(false);
    base.SiteUser = ko.observable(new SiteUserVm());
    base.IsInitialized = ko.observable(false);
    base.DebugMode = ko.observable(true);
    base.ViewModelMarkup = ko.observable(null);

    base.GetData = function(url, parameters) {
        var deferred = $.Deferred();
        base.PerformingServerCall(true);
        $.ajax({
                url: url,
                cache: false,
                data: parameters,
                dataType: "json",
                contentType: "application/json charset=utf-8"
            }).done(function(data) {
                deferred.resolve(data);
            }).fail(function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
                deferred.reject();
            })
            .always(function() {
                base.PerformingServerCall(false);
            });
        return deferred;
    };

    base.PostUpdateToViewModelAndRefresh = function(url, dataToPostVm, mapToJsonFunc, stringifyOutput) {
        var deferred = $.Deferred();
        
        var dataToPostVmValues = mapToJsonFunc != null
            ? mapToJsonFunc(dataToPostVm)
            : ko.mapping.toJS(dataToPostVm);

        var postData;
        if (stringifyOutput) {
            postData = JSON.stringify(dataToPostVmValues);
        } else {
            postData = dataToPostVmValues;
        }

        base.PerformingServerCall(true);
        $.post(url, postData)
            .done(function(data) {
                    var parsedJson = JSON.parse(data);
                    ko.mapping.fromJS(parsedJson, {}, dataToPostVm);
                    deferred.resolve(dataToPostVm);
                })
            .fail(function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
                deferred.reject();
            })
            .always(function() {
                base.PerformingServerCall(false);
            });
        return deferred;
    };

    base.ToggleViewModelMarkup = function() {
        if (base.ViewModelMarkup() == null)
            base.ViewModelMarkup(ko.toJSON(this, null, 2));
        else
            base.ViewModelMarkup(null);
    };

    base.GetQueryStrings = function() {
        var assoc = {};
        var decode = function(s) { return decodeURIComponent(s.replace(/\+/g, " ")); };
        var queryString = location.search.substring(1);
        var keyValues = queryString.split('&');

        for (var i in keyValues) {
            var key = keyValues[i].split('=');
            if (key.length > 1) {
                assoc[decode(key[0])] = decode(key[1]);
            }
        }

        return assoc;
    };

    /*base.GetBaseData = function() {
        base.GetData("/Users/GetSiteUserInfo").done(
            function(data) {
                var parsedData = JSON.parse(data);
                base.SiteUser(new SiteUserVm(parsedData.User));
                base.DebugMode(parsedData.DebugMode);
                base.IsInitialized(true);
            });
    };*/
    //base.GetBaseData();
}